/**
 * @a https://leetcode.cn/problems/longest-palindromic-substring/
 */

#include "common.h"

class Solution {
public:
    string longestPalindrome(string s) {
        int sz = s.size();
        if(sz == 0) return "";
        if(sz == 1) return {s[0]};
        int maxlen = 0;
        int left = 0, right = 0, ret = 0;
        for(int i = 0; i < sz; ++i){
            right = left = i;
            while(right < sz - 1 && s[left] == s[right+1]) ++right;
            while(left > 0 && right < sz - 1 && s[left - 1] == s[right + 1]) {
                --left;
                ++right;
            }
            if((right - left + 1) > maxlen){
                maxlen = (right - left + 1);
                ret = left;
            }
        }
        return s.substr(ret, maxlen);
    }
};